﻿import QtQuick 2.4
import QtQuick.Window 2.2
import QtQuick.Controls 1.3

Item
{
    id:rootAppR
    width: 412
    height: 700
    property int keys: 7
    property string nameInfo:"AQI"
    transform: Scale { origin.x: 0; origin.y: 0; xScale: root.x_enl-0.3;yScale: root.y_enl-0.4}
//     transform: Scale { origin.x: 0; origin.y: 0; xScale: root.x_enl;yScale: root.y_enl}
    APPCloseMenu{
        anchors.top: parent.top
        anchors.right: parent.right
        anchors.topMargin: 7
        anchors.rightMargin: 12
        onClose: {

            rootAppR.visible =false
            raning.visible = false
            emit: colseSiteRanking()
        }

    }

    Rectangle
    {
        width: parent.width
        height: parent.height
        z:-1
        color: app.getSystemThemeColor()
        radius: 5
    }


    //标题item

    Item
    {

        id:titleRec
        anchors.top: parent.top
        width: parent.width
        height: 46
        Text
        {
            id:tiltle
            text:"全省" +nameInfo +"排行"
            font.family: "微软雅黑"
            font.pixelSize: 20
            color: "white"
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.top: parent.top
            anchors.topMargin: 17

        }

    }

    //白线
    Rectangle
    {
        width:360
        height: 1
        anchors.top:titleRec.bottom
        anchors.topMargin: 20
        anchors.horizontalCenter:   parent.horizontalCenter
        color: "#A0A0A0"
    }

    ListModel
    {
        id:panModel
        Component.onCompleted:
        {
            onpenDateLise()
        }
    }

    function onpenDateLise()
    {
        if(panModel.count ===0)
        {

        }else
        {
            panModel.clear()
        }

        var sort_by = function(field, reverse, primer){ //定义排序方法
            var key = primer ?
                        function(x) {return primer(x[field])} :
            function(x) {return x[field]};
            reverse = [-1, 1][+!!reverse];
            return function (a, b) {
                return a = key(a), b = key(b), reverse * ((a > b) - (b > a));
            }
        }
        var planData_ = mainApp.slotGetCityRankingInfo();
        var json_ = JSON.parse(planData_);
        var test1_sort;
        if(json_.length === 0)
        {}
        else
        {
            test1_sort = json_.sort(sort_by('value', false, parseFloat)); //根据total，降序排
//            test1_sort = json_.sort(sort_by('value', false, parseInt)); //根据total，降序排
            var tempMax = test1_sort[0].value
            var tempMin = test1_sort[test1_sort.length-1].value
            if (test1_sort.length=== 0)
            {

            }else
            {
                for( var i = 0; i < test1_sort.length; i++)
                {

                    var y  = mapValue(test1_sort[i].value,tempMin,test1_sort[i].key)
                    var u
                    if(test1_sort[i].name.length === 3)
                    {
                        if(i+1 >=10)
                        {
                            u =i+1 + "    "+test1_sort[i].name
                        }else
                        {
                            u =i+1 + "      "+test1_sort[i].name
                        }

                    }else
                    {
                        if(i+1 >=10)
                        {
                            u =i+1 + "    "+test1_sort[i].name
                        }else
                        {
                            u =i+1 + "      "+test1_sort[i].name
                        }
                    }
                    keys = parseInt(test1_sort[i].key)

                    if(keys ===7 )
                    {
                        nameInfo ="AQI"
                    }else if(keys ===6)
                    {
                         nameInfo ="O3"
                    }else if (keys ===5)
                    {
                        nameInfo ="CO"
                    }else if(keys ===4)
                    {
                         nameInfo ="NO2"
                    }else if(keys ===3)
                    {
                         nameInfo ="SO2"
                    }else if(keys ===2)
                    {
                         nameInfo ="PM2.5"
                    }else if(keys ===1)
                    {
                         nameInfo ="PM10"
                    }
                    var uu  = ranInfo(test1_sort[i].name)
                   panModel.append({"name":u,"na":test1_sort[i].name,"navalue":uu,"value":test1_sort[i].value,"lent":y,"key":test1_sort[i].key})
                }
            }
        }
    }
    function mapValue(x,z,u)
            {
                var value = parseFloat(x)
                var max_v

                var min_v = parseFloat(z)
                var key = parseInt(u)
//                var m_max = 302
                 var m_max = 240
                var a
                var b
                switch(key)
                {
                case 1:
                    max_v = 420
                    break;
                case 2:
                    max_v = 250
                    break;
                case 3:
                    max_v = 1600
                    break;
                case 4:
                    max_v = 565
                    break;
                case 5:
                    max_v = 36
                    break;
                case 6:
                    max_v = 800
                    break;
                case 7:
                    max_v = 300
                    break;
                }
                if (min_v > max_v)
                {
//                    return 302
                    return 240
                }
                if(value <max_v)
                {

//                    if(max_v >302)
//                    {
//                        a = max_v/(302 -40)
//                        return value/a  +40
//                    }else
//                    {
//                        a = (302 -40)/max_v
//                        return value*a  +40
//                    }
                    if(max_v >240)
                    {
                        a = max_v/(240 -20)
                        return value/a  +20
                    }else
                    {
                        a = (240 -20)/max_v
                        return value*a  +20
                    }
                }
//                else
//                {
//                    return 302
//                }
                else
                {
                    return 240
                }
            }
    function ranInfo(m)
    {
        if(m==="郑州")
        {
            return "0"
        }else if(m==="开封")
        {
            return "1"
        }else if(m==="洛阳")
        {
            return "2"
        }else if(m==="平顶山")
        {
            return "3"
        }else if(m==="安阳")
        {
            return "4"
        }else if(m==="鹤壁")
        {
            return "5"
        }else if(m==="新乡")
        {
            return "6"
        }else if(m==="焦作")
        {
            return "7"
        }else if(m==="濮阳")
        {
            return "8"
        }else if(m==="许昌")
        {
            return "9"
        }else if(m==="漯河")
        {
            return "10"
        }else if(m==="三门峡")
        {
            return "11"
        }else if(m==="商丘")
        {
            return "12"
        }else if(m==="周口")
        {
            return "13"
        }else if(m==="驻马店")
        {
            return "14"
        }else if(m==="南阳")
        {
            return "15"
        }else if(m==="信阳")
        {
            return "16"
        }
    }

    //显示Item
    Item
    {
        id:date
        width: parent.width-20
        height: parent.height -40
        anchors.top:titleRec.bottom

         anchors.topMargin: 40
        anchors.left: parent.left
//        anchors.leftMargin: 20
        anchors.leftMargin: 30

        ScrollView
        {
            width: parent.width
            height: parent.height

            ListView
            {
                id:panView
                spacing: 10
                model:panModel
                delegate: panDele
                width: parent.width
                clip: true;
                anchors.fill: parent
                cacheBuffer: 200
            }
        }


    }
    //代理
    Component
    {
        id:panDele
        Row
        {
            id:panDeleRow
            spacing:
            {
                if(index+1 >= 10)
                {
                    if(testNa.text.length ===8)
                    {
//                        return 23
                        return 33
                    }else if(testNa.text.length ===9)
                    {
//                        return 8
                        return 18
                    }
                }else
                {
                    if(testNa.text.length ===8)
                    {
//                        return 30
                        return 40
                    }else if(testNa.text.length ===9)
                    {
//                        return 22
                        return 32
                    }else if (testNa.text.length ===10)
                    {
                        return 18
//                        return 8
                    }
                }

//                if(name.length >2)
//                {
//                    return 14
//                }else
//                {

//                    return 30
//                }
//                if(name.length ===7)
//                {
//                    return 30
//                }else if(name.length ===8)
//                {
//                    return 22
//                }else if (name.length ===9)
//                {

//                    return 14
//                }
            }
            Text
            {
                id:testNa
                text: name
                font.family: "微软雅黑"
//                font.pixelSize: 11
                font.pixelSize: 14
                color: "white"

                MouseArea
                {
                    anchors.fill: parent
                    onClicked:
                    {
//                        name
                        root.cityRan = navalue
                        root.passName = na
                        emit:openAPPDetaShow()
                    }
                }
            }
            Column{
                Rectangle
                {
                    width: colorBanner.width
                    height: 3
                    color: "transparent"
                }

                Rectangle
                {
                    id:colorBanner
                    width:0
                    radius: 1.5
                    height: testNa.height -3
                    color: {
                        //pm10
                        if(parseInt(key) ===1 )//7
                        {
                            var i = parseInt(value)
                            if(i <= 50)
                            {
//                                return "#00E400"
                                return "#57d01a"
                            }else if(i > 50 &&  i<= 150)
                            {
//                                return "#FFFF00"
                                return "#f4dc2c"
                            }else if (i > 150 &&  i<= 250)
                            {
//                                return "#FF7E00"
                                return "#eb881e"
                            }else if (i > 250 &&  i<= 350)
                            {
//                                return "#FF0000"
                                return "#f15916"
                            }else if (i >350 &&  i<= 420)
                            {
//                                return "#99004C"
                                return "#fe1a1a"
                            }else if (i > 420)
                            {
//                                return "#7E0023"
                                return "#b40055"

                            }
                        }
                        //pm2.5
                        else if(parseInt(key) ===2)//2
                        {
                            var i = parseInt(value)
                            if(i <= 35)
                            {
//                                return "#00E400"
                                return "#57d01a"
                            }else if(i > 35 &&  i<= 75)
                            {
//                                 return "#FFFF00"
                                return "#f4dc2c"
                            }else if (i > 75 &&  i<= 115)
                            {
//                                return "#FF7E00"
                                return "#eb881e"
                            }else if (i > 115 &&  i<= 150)
                            {
//                                return "#FF0000"
                                return "#f15916"
                            }else if (i >150 &&  i<= 250)
                            {
//                                return "#99004C"
                                return "#fe1a1a"
                            }else if (i > 250)
                            {
//                                 return "#7E0023"
                                return "#b40055"
                            }
                        }else if (parseInt(key) ===3)//so2 1
                        {
                            var i = parseInt(value)
                            if(i <= 50)
                            {
//                                return "#00E400"
                                return "#57d01a"
                            }else if(i > 50 &&  i<= 150)
                            {
//                                return "#FFFF00"
                                return "#f4dc2c"
                            }else if (i > 150 &&  i<= 475)
                            {
//                                return "#FF7E00"
                                 return "#eb881e"
                            }else if (i > 475 &&  i<= 800)
                            {
//                                return "#FF0000"
                                return "#f15916"
                            }else if (i >800 &&  i<= 1600)
                            {
//                                return "#99004C"
                                return "#fe1a1a"
                            }else if (i > 1600)
                            {
//                                 return "#7E0023"
                                return "#b40055"
                            }
                        }else if(parseInt(key) ===4)//no2 3
                            {

                            var i = parseInt(value)
                            if(i <= 40)
                            {
//                                return "#00E400"
                                return "#57d01a"
                            }else if(i > 40 &&  i<= 80)
                            {
//                                return "#FFFF00"
                                return "#f4dc2c"
                            }else if (i > 80 &&  i<= 180)
                            {
//                                return "#FF7E00"
                                 return "#eb881e"
                            }else if (i > 180 &&  i<= 280)
                            {
//                                return "#FF0000"
                                return "#f15916"
                            }else if (i >280 &&  i<= 565)
                            {
//                                return "#99004C"
                                return "#fe1a1a"
                            }else if (i > 565)
                            {
//                                return "#7E0023"
                                return "#b40055"
                            }
                        }else if(parseInt(key) ===5)//co 4
                        {
                            var i = parseInt(value)
                            if(i <= 2)
                            {
//                                  return "#00E400"
                                return "#57d01a"
                            }else if(i > 2 &&  i<= 4)
                            {
//                                return "#FFFF00"
                                return "#f4dc2c"
                            }else if (i > 4 &&  i<= 14)
                            {
//                                return "#FF7E00"
                                return "#eb881e"
                            }else if (i > 14 &&  i<= 24)
                            {
//                                 return "#FF0000"
                                return "#f15916"
                            }else if (i >24 &&  i<= 36)
                            {
//                                return "#99004C"
                                return "#fe1a1a"
                            }else if (i > 36)
                            {
//                                return "#7E0023"
                                return "b40055"
                            }
                        }else if(parseInt(key) ===6)//o3 5
                        {
                            var i = parseInt(value)
                            if(i<= 100)
                            {
//                                return "#00E400"
                                return "#57d01a"
                            }else if(i > 100 &&  i<= 160)
                            {
//                                return "#FFFF00"
                                return "#f4dc2c"
                            }else if (i > 160 &&  i<= 215)
                            {
//                                return "#FF7E00"
                                return "#eb881e"
                            }else if (i > 215 &&  i<= 265)
                            {
//                                 return "#FF0000"
                                return "#f15916"
                            }else if (i >265 &&  i<= 800)
                            {
//                                return "#99004C"
                                return "#fe1a1a"
                            }else if (i > 800)
                            {
//                                return "#7E0023"
                                return "#b40055"
                            }
                        }else if(parseInt(key) ===7)//api 6
                        {
                            var i = parseInt(value)
                            if(i <= 50)
                            {
//                                 return "#00E400"
                                return "#57d01a"
                            }else if(i > 50 &&  i<= 100)
                            {
//                                 return "#FFFF00"
                                return "#f4dc2c"
                            }else if (i > 100 &&  i<= 150)
                            {
//                                return "#FF7E00"
                                return "#eb881e"
                            }else if (i > 150 &&  i<= 200)
                            {
//                                return "#FF0000"
                                return "#f15916"
                            }else if (i >200 &&  i<= 300)
                            {
//                                return "#99004C"
                                return "#fe1a1a"
                            }else if (i > 300)
                            {
//                                return "#7E0023"
                                return "#b40055"
                            }
                        }
                   }
                    Text
                    {
                        text:value
//                        color:"black"
                        color:
                            {
                            //pm10
                            if(parseInt(key) ===1 )//7
                            {
                                var i = parseInt(value)
                                if(i <= 50)
                                {
    //                                return "#00E400"
                                    return "#444444"
                                }else if(i > 50 &&  i<= 150)
                                {
    //                                return "#FFFF00"
                                    return "#444444"
                                }else if (i > 150 &&  i<= 250)
                                {
    //                                return "#FF7E00"
                                    return "#444444"
                                }else if (i > 250 &&  i<= 350)
                                {
    //                                return "#FF0000"
                                    return "#444444"
                                }else if (i >350 &&  i<= 420)
                                {
    //                                return "#99004C"
                                    return "#dddddd"
                                }else if (i > 420)
                                {
    //                                return "#7E0023"
//                                    return "#b40055"
                                    return "#dddddd"
                                }
                            }
                            //pm2.5
                            else if(parseInt(key) ===2)//2
                            {
                                var i = parseInt(value)
                                if(i <= 35)
                                {
                                    return "#444444"
                                }else if(i > 35 &&  i<= 75)
                                {
                                     return "#444444"
                                }else if (i > 75 &&  i<= 115)
                                {
                                    return "#444444"
                                }else if (i > 115 &&  i<= 150)
                                {
                                    return "#444444"
                                }else if (i >150 &&  i<= 250)
                                {
                                    return "#dddddd"
                                }else if (i > 250)
                                {
                                     return "#dddddd"
                                }
                            }else if (parseInt(key) ===3)//so2 1
                            {
                                var i = parseInt(value)
                                if(i <= 50)
                                {
                                    return "#444444"
                                }else if(i > 50 &&  i<= 150)
                                {
                                    return "#444444"
                                }else if (i > 150 &&  i<= 475)
                                {
                                    return "#444444"
                                }else if (i > 475 &&  i<= 800)
                                {
                                    return "#444444"
                                }else if (i >800 &&  i<= 1600)
                                {
                                    return "#dddddd"
                                }else if (i > 1600)
                                {
                                     return "#dddddd"
                                }
                            }else if(parseInt(key) ===4)//no2 3
                                {

                                var i = parseInt(value)
                                if(i <= 40)
                                {
                                    return "#444444"
                                }else if(i > 40 &&  i<= 80)
                                {
                                    return "#444444"
                                }else if (i > 80 &&  i<= 180)
                                {
                                    return "#444444"
                                }else if (i > 180 &&  i<= 280)
                                {
                                    return "#444444"
                                }else if (i >280 &&  i<= 565)
                                {
                                    return "#dddddd"
                                }else if (i > 565)
                                {
                                    return "#dddddd"
                                }
                            }else if(parseInt(key) ===5)//co 4
                            {
                                var i = parseInt(value)
                                if(i <= 2)
                                {
                                      return "#444444"
                                }else if(i > 2 &&  i<= 4)
                                {
                                    return "#444444"
                                }else if (i > 4 &&  i<= 14)
                                {
                                    return "#444444"
                                }else if (i > 14 &&  i<= 24)
                                {
                                     return "#444444"
                                }else if (i >24 &&  i<= 36)
                                {
                                    return "#dddddd"
                                }else if (i > 36)
                                {
                                    return "#dddddd"
                                }
                            }else if(parseInt(key) ===6)//o3 5
                            {
                                var i = parseInt(value)
                                if(i<= 100)
                                {
                                    return "#444444"
                                }else if(i > 100 &&  i<= 160)
                                {
                                    return "#444444"
                                }else if (i > 160 &&  i<= 215)
                                {
                                    return "#444444"
                                }else if (i > 215 &&  i<= 265)
                                {
                                     return "#444444"
                                }else if (i >265 &&  i<= 800)
                                {
                                    return "#dddddd"
                                }else if (i > 800)
                                {
                                    return "#dddddd"
                                }
                            }else if(parseInt(key) ===7)//api 6
                            {
                                var i = parseInt(value)
                                if(i <= 50)
                                {
                                    return "#444444"
                                }else if(i > 50 &&  i<= 100)
                                {
                                    return "#444444"
                                }else if (i > 100 &&  i<= 150)
                                {
                                    return "#444444"
                                }else if (i > 150 &&  i<= 200)
                                {
                                    return "#444444"
                                }else if (i >200 &&  i<= 300)
                                {
                                    return "#dddddd"
                                }else if (i > 300)
                                {
                                    return "#dddddd"
                                }
                            }
                        }

                        anchors.verticalCenter: colorBanner.verticalCenter
//                        anchors.verticalCenterOffset: 4
//                        anchors.horizontalCenter: parent.horizontalCenter
//                        anchors.horizontalCenterOffset:-(parent.width/2) +18
                        anchors.left: parent.left
                        anchors.leftMargin:  4
//                        anchors.bottom: parent.bottom
//                        anchors.bottomMargin: 0.0001
//                        anchors.centerIn: parent.Center
                        font.pixelSize: 14
                        font.family: "微软雅黑"

                    }


                }
                //滑动
                PropertyAnimation {
                    id: animation;
                    target: colorBanner;
                    property: "width";
                    to: {
                        return parseFloat(lent)
                    }
                    duration: 1000;
                    running :true
                }

                Rectangle
                {
                    width: colorBanner.width
                    height: 3
                    color: "transparent"
                }
            }
        }
    }

    Connections
    {
        target: mainApp
        onSignalCityRanking:
        {
            onpenDateLise();
        }
    }


}






















